package mytest;

public class TopCoder2 {

	/**
	 * We have a sequence of integers, and we would like to 
	 * remove all duplicate elements from this sequence. 
	 * There may be multiple ways to perform this task. For example, 
	 * given the sequence { 1, 2, 1, 3 }, we could end up with either { 1, 2, 3 } or { 2, 1, 3 } as 
	 * the remaining sequence, depending on which duplicate 1 we remove from the original 
	 * sequence. For this problem, we want to return the lexicographically 
	 * first of of all possible remaining sequences. A sequence S1 comes before 
	 * sequence S2 lexicographically if and only if S1 has a smaller value than S2 at the lowest 
	 * index at which the two sequences differ (so, for example, { 1, 2, 3 } comes before { 2, 3, 1 }).
	  You will be given a int[] sequence. Return a int[] containing the sequence after all the duplicates are removed. See the examples for further clarification.
	 * 
	 * 
	 * 
	 * 
	 */
	
	
	static void solve(int a[])
	{
		int s[]=new int[1001];
		int t[]=new int[1001];
		int max=0;
		for(int i=0;i<a.length;i++)
		{
			s[a[i]]++;
			t[a[i]]=1;
			if(max<a[i])max=a[i];
		}
		for(int i=1;i<=max;i++)
		{
			t[i]=t[i-1]+t[i];
		}
		
		int cur=1;
		System.out.print("{");
		for(int i=0;i<a.length;i++)
		{
			if(s[a[i]]==1)
			{
				System.out.print((cur>1?", ":"")+a[i]);
				s[a[i]]=0;
				cur++;
			}else if(s[a[i]]>0&&t[a[i]]<=cur)
			{
				System.out.print((cur>1?", ":"")+ a[i]);
				s[a[i]]=0;
				cur++;
			}else s[a[i]]--;
		}
		System.out.print(" }");
	}
	
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int a[]= {3, 2, 4, 2, 4, 4};
		
		solve(new int[]{1, 3, 2, 4, 2, 3});
		
		
	}

}
